package com.kust.ucenter.controller;


import com.kust.commomUtils.JwtUtils;
import com.kust.commomUtils.Result;
import com.kust.serviceBase.exceptionHandler.GuliException;
import com.kust.serviceBase.orderVo.memberOrder;
import com.kust.ucenter.entity.Member;
import com.kust.ucenter.entity.vo.LoginVo;
import com.kust.ucenter.entity.vo.RegisterVo;
import com.kust.ucenter.service.MemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-06-30
 */
@RestController
@RequestMapping("/ucenter/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    //登录
    @PostMapping("/login")
    public Result loginUser(@RequestBody LoginVo loginVo) {
        //调用service方法实现登录
        //返回token值
        String token=memberService.login(loginVo);
        return Result.ok().data("token",token);
    }


    //注册
    @PostMapping("/register")
    public Result registerUser(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return Result.ok();
    }


    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("/auth/getLoginInfo")
    public Result getLoginInfo(HttpServletRequest request){
        try {
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            LoginVo loginInfoVo = memberService.getLoginInfo(memberId);
            return Result.ok().data("item", loginInfoVo);
        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20001,"error");
        }
    }

    @ApiOperation("根据用户id来查询对象")
    @PostMapping("getUserInfoOrder/{id}")
    public memberOrder getUserInfoOrder(@PathVariable("id") String id){
        Member member = memberService.getById(id);
        //吧member对象复制给memberOrder对象
        memberOrder memberOrder = new memberOrder();
        BeanUtils.copyProperties(member,memberOrder);
        return memberOrder;
    }

    @GetMapping(value = "countregister/{day}")
    public Result registerCount(
            @PathVariable String day){
        Integer count = memberService.countRegisterByDay(day);
        return Result.ok().data("countRegister", count);
    }
}

